from newsgraf.reader.models import Story
from django.shortcuts import render_to_response
from settings import CATEGORIES
from datetime import *

def index(request):
  #  story_list = Story.objects.order_by('date_time')[:50]

  stories = {}
  now = datetime.now()
  for category in CATEGORIES:
    print category
    search_kwargs = {'category': category, 'date_time__gt': now - timedelta(days=7)}
    story_list = Story.objects.order_by('priority').filter(**search_kwargs)
    if category == 'news':
      story_list = story_list[:2]
    else:
      story_list = story_list[:5]
    for story in story_list:
      story.age = now - story.date_time
    stories[category] = filter(lambda story: story.category == category, story_list)
  
  return render_to_response('frontpage.html', {'stories': stories})
